package yxy.game.pm2.command.niubee;

import yxy.cherry.data.DataCenter;
import yxy.cherry.data.bean.weaponskillcost;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.currency.CurrencyNotEnoughException;
import yxy.game.pm2.bean.mq.CCReason;
import yxy.game.pm2.bean.pubsub.CurrencyPublisher;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.NiuBeeMessage;
import yxy.game.pm2.module.CurrencyHandle;
import yxy.game.pm2.module.niubee.NiuBeeModule;
import yxy.game.pm2.module.tip.TipHandle;
import yxy.game.pm2.module.tip.TipType;

import java.util.Collection;

/** 神器 神器技能升级 */
@MessageType(proto = Command.NiuBeeAbilityUpgrade, classtype = NiuBeeMessage.NiuBeeAbilityUpgradeRequest.class)
public class CMD_NiuBeeAbilityUpgrade extends Command<NiuBeeMessage.NiuBeeAbilityUpgradeRequest> {

	@Override
	public void execute(MessageSource messageSource, NiuBeeMessage.NiuBeeAbilityUpgradeRequest request) {
		String playerCode = messageSource.getTag();

		NiuBeeMessage.NiuBeeAbilityUpgradeResponse.Builder response = NiuBeeMessage.NiuBeeAbilityUpgradeResponse.newBuilder();

		NiuBeeModule niuBeeModule = NiuBeeModule.get(playerCode);
		try {
			if (niuBeeModule.tryLock()) {
				int skilllevel = niuBeeModule.getSkillLevel();
				weaponskillcost data = DataCenter.getData(skilllevel + 1, weaponskillcost.class);
				if (data == null) {
					send(messageSource, response, Result.NiubeeSkillUpper);// 技能满级
					return;
				}
				data = DataCenter.getData(skilllevel, weaponskillcost.class);
				if (data == null) {
					send(messageSource, response, Result.DataError);
					return;
				}
				Collection<Currency> cost = Currency.create(data.getCosttype(), data.getCostid(), data.getCostnum());
				try {
					new CurrencyHandle().cost(playerCode, CCReason.神器, cost);
				} catch (CurrencyNotEnoughException e) {
					send(messageSource, response, new Result(Result.CostFail, e.getType().getNumber(), e.getDataId()));// 消费不足
					return;
				}
				niuBeeModule.setSkillLevel(niuBeeModule.getSkillLevel() + 1);
				niuBeeModule.set();

				response.setSkillLevel(niuBeeModule.getSkillLevel());
				send(messageSource, response, Result.Success);

				CurrencyPublisher.getInstance().publishMinus(playerCode, cost);
				if (DataCenter.getData(niuBeeModule.getSkillLevel() + 1, weaponskillcost.class) == null) {
					new TipHandle().updateTip(playerCode, TipType.神器_技能切页, false); // 技能满级
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			send(messageSource, response, Result.DataError);
		} finally {
			niuBeeModule.unlock();
		}
	}

	private void send(MessageSource messageSource, NiuBeeMessage.NiuBeeAbilityUpgradeResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}
}